package com.wframe.project.jnd.dao;

import com.frame.database.JdataBean;
import com.frame.database.PageResult;
import com.frame.util.DateConvert;
import com.wframe.framework.database.BaseDao;
import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Repository;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * Created by Administrator on 2017/7/31.
 */
@Repository
public class IntegralDao extends BaseDao {
    public PageResult queryIntegralList(JdataBean parms, int pagesize, int pageno) {
        String sql = "select * from wb_integral where 1 = 1";
        List parmlist = new ArrayList();
        if (parms != null) {
            if (StringUtils.isNotBlank(parms.getString("userid"))) {
                sql += " and userid=?";
                parmlist.add(parms.getString("userid"));
            }
            if (StringUtils.isNotBlank(parms.getString("status")) && parms.getInt("status") == 1) {
                sql += " and integral >= 0";
            }
            if (StringUtils.isNotBlank(parms.getString("status")) && parms.getInt("status") == 2) {
                sql += " and integral < 0";
            }
            if (parms.get("start") != null) {
                sql += " and create_time >= ?";
                parmlist.add(parms.get("start"));
            }
            if (parms.getString("end") != null) {
                sql += " and create_time <= ?";
                parmlist.add(parms.get("end"));
            }
        }
        sql += " order by id desc";
        return querySqlMap2(sql, parmlist, pagesize, pageno);
    }

    public List<JdataBean> queryWXIntegralList(String userid) {
        if(StringUtils.isBlank(userid)){
            return new ArrayList<JdataBean>();
        }
        ArrayList<Object> list = new ArrayList<>();
        Date now = new Date();
        list.add(userid);
        list.add(DateConvert.addMonth(now, -6));
        list.add(now);
        String sql = "select integral, content, create_time from wb_integral where userid = ? and create_time >= ? and create_time <= ? order by id desc";
        return querySql2(sql, list);
    }

    public JdataBean queryUserIntegrals(String userid) {
        String sql = "select DATE_FORMAT(NOW(),'%Y') as now_year,integral,ly_integral,this_year,(select SUM(get_integral) from wb_order_list where userid = ? and `status` = 11 and integral_state = 1) as store_integral from wbuser where id = ?";
        return  getObject(sql,new Object[]{userid, userid});
    }

    public List<JdataBean> queryExchangeCoupon() {
        String sql = "select c.id,c.min_money,c.offer_money,c.activity_day,c.amount,c.sales from wb_coupon c where c.tag = 0 and c.state = 1 and c.exchange = 1 order by c.offer_money asc";
        return querySql2(sql,null);
    }

    public JdataBean queryExchangeCoupon(String couponid) {
        String sql = "select c.* from wb_coupon c where c.id = ? and c.tag = 0 and c.state = 1 and c.exchange = 1 and c.amount > c.sales";
        return getObject(sql, new Object[]{couponid});
    }

    public List<JdataBean> getIntegralRules() {
        return querySql("select p.* from wb_sys_pro p", null);
    }
}
